package com.ruoyi.web.controller.combat.useralignment;


import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.useralignment.CombatAlignmentTask;
import com.ruoyi.combat.domain.useralignment.CombatAlignmentWarn;
import com.ruoyi.combat.domain.useralignment.CombatRelationWb;
import com.ruoyi.combat.domain.useralignment.CombatUserWb;
import com.ruoyi.combat.mapper.useralignment.CombatAlignmentTaskMapper;
import com.ruoyi.combat.mapper.useralignment.CombatAlignmentWarnMapper;
import com.ruoyi.combat.mapper.useralignment.CombatRelationWbMapper;
import com.ruoyi.combat.mapper.useralignment.CombatUserWbMapper;
import com.ruoyi.combat.util.Topic5Util;
import com.ruoyi.combat.util.weibo.WeiboApiUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 课题五算法回调接口Controller
 *
 * @author ruoyi
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/combat/callback")
@Anonymous
@Slf4j
public class CombatCallBackController extends BaseController {

    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private CombatAlignmentTaskMapper combatAlignmentTaskMapper;
    @Autowired
    private CombatUserWbMapper combatUserWbMapper;
    @Autowired
    private CombatRelationWbMapper combatRelationWbMapper;

    @Autowired
    private WeiboApiUtil weiboApiUtil;

    /**
     * 社团成员抗识别
     * @param data
     * @return
     */
    @PostMapping("teamResult")
    public AjaxResult teamResult(@RequestBody Map data) {

        return success();
    }

    /**
     * 识别结果
     * @param data
     * @return
     */
    @PostMapping("recognitionResult")
    public AjaxResult recognitionResult(@RequestBody Map data) {
        log.info("识别算法回调结果",data);
        if(data.get("sim")!=null&&data.get("taskId")!=null) {
            Map map= (Map) data.get("sim");
            String jsonStr = JSONUtil.toJsonStr(map);
            String align = String.valueOf(data.get("align"));

            CombatAlignmentWarn combatAlignmentWarn = combatAlignmentWarnMapper.selectCombatAlignmentWarnById(Long.parseLong(String.valueOf(data.get("taskId"))));
            combatAlignmentWarn.setSimilarContent(jsonStr);
            combatAlignmentWarn.setResult(align);
            combatAlignmentWarnMapper.updateCombatAlignmentWarn(combatAlignmentWarn);
        }
        return success();
    }

    /**
     * 计算分数回调
     * @param data
     * @return
     */
    @PostMapping("GetScore")
    public AjaxResult GetScore(@RequestBody Map data) {
        return success();
    }




    /**
     * 抗识别结果
     * @param data
     * @return
     */
    @PostMapping("antiRelevancy")
    public AjaxResult antiRelevancy(@RequestBody Map data) {
        log.info("***************抗识别结果*******************");
        String warnId = String.valueOf(data.get("warnId"));
        List<String> addNodes = (List<String>) data.get("addNode");
        List<Map<String,String>> addRelationship = (List<Map<String, String>>) data.get("addRelationship");
        String jsonStr = JSONUtil.toJsonStr(addRelationship);
        CombatAlignmentTask task = new CombatAlignmentTask();
        task.setWarnId(warnId);
        task.setTaskstate("1");
        List<CombatAlignmentTask> combatAlignmentTasks = combatAlignmentTaskMapper.selectCombatAlignmentTaskList(task);
        CombatAlignmentWarn combatAlignmentWarn1 = combatAlignmentWarnMapper.selectCombatAlignmentWarnById(Long.parseLong(warnId));
        if(combatAlignmentTasks.size()>0) {
            CombatAlignmentTask combatAlignmentTask = combatAlignmentTasks.get(0);
            String fromUserId = combatAlignmentTask.getFromUserId();
            CombatRelationWb combatRelationWb = new CombatRelationWb();
            combatRelationWb.setUserid(fromUserId);
            List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);


            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(fromUserId);
            String token = combatUserWb.getToken();
            //添加微博关注
            if(StringUtils.isNotEmpty(token)){
                weiboApiUtil.tofollow(fromUserId,addNodes);

                List<Map<String, String>> listMap = new ArrayList<>();
                Map<String, String> da = new HashMap<>();
                da.put("type", "follow");
                da.put("cookie", token);
                da.put("user_id", fromUserId);
                listMap.add(data);
                weiboApiUtil.getFriends(listMap);
            }
            List<Map<String, String>> listMap = new ArrayList<>();
            Map<String, String> da = new HashMap<>();
            da.put("type", "follow");
            da.put("cookie", combatUserWb.getToken());
            da.put("user_id", fromUserId);
            listMap.add(da);
            weiboApiUtil.getFriends(listMap);

            Map<String, Object> params = new HashMap<>();
            params.put("initData",combatRelationWbs);
            params.put("nowData",addNodes);

            combatAlignmentTask.setTaskResult(JSONUtil.toJsonStr(params));
            combatAlignmentTask.setTaskstate("2");
            combatAlignmentTask.setEndtime(new Date());
            combatAlignmentTask.setResult(combatAlignmentWarn1.getResult());
            combatAlignmentTaskMapper.updateCombatAlignmentTask(combatAlignmentTask);
        }
        CombatAlignmentWarn combatAlignmentWarn = new CombatAlignmentWarn();
        combatAlignmentWarn.setId(Long.parseLong(warnId));
        combatAlignmentWarn.setStatus("1");
        combatAlignmentWarnMapper.updateCombatAlignmentWarn(combatAlignmentWarn);
        return success();
    }





}
